<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:valid="xalan://by.epam.xslttask.validator.ProductValidator">
    <xsl:output indent="yes"/>
    <xsl:param name="subcategory"/>
    <xsl:param name="category"/>
    <xsl:param name="notForm"/>
    <xsl:param name="name"/>
    <xsl:param name="producer"/>
    <xsl:param name="model"/>
    <xsl:param name="date"/>
    <xsl:param name="color"/>
    <xsl:param name="price"/>
    <xsl:param name="notInStock"/>
    <xsl:param name="error"/>

    <xsl:variable name="validator" select="valid:new()"/>
    <xsl:param name="isValid" select="valid:validate($validator, $name, $producer, $model, $date, $color, $price, $error)"/>
    <!--<xsl:param name="errorList" select="valid:getErrors($validator)"/>-->

    <xsl:template match="/">
        <xsl:choose>
            <xsl:when test="$notForm = '1'">
                <xsl:apply-templates/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="html-page"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:choose>
            <xsl:when test="$isValid">
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="html-page"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="subcategory">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
            <xsl:if test="$isValid">
                <xsl:if test="../@name=$category and @name=$subcategory">
                    <xsl:element name="goods">
                        <xsl:attribute name="name">
                            <xsl:value-of select="$name"/>
                        </xsl:attribute>
                        <xsl:element name="producer">
                            <xsl:value-of select="$producer"/>
                        </xsl:element>
                        <xsl:element name="model">
                            <xsl:value-of select="$model"/>
                        </xsl:element>
                        <xsl:element name="date">
                            <xsl:value-of select="$date"/>
                        </xsl:element>
                        <xsl:element name="color">
                            <xsl:value-of select="$color"/>
                        </xsl:element>
                        <xsl:choose>
                            <xsl:when test="$notInStock">
                                <xsl:element name="notInStock"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:element name="price">
                                    <xsl:value-of select="$price"/>
                                </xsl:element>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:element>
                </xsl:if>
            </xsl:if>
        </xsl:copy>
    </xsl:template>

    <xsl:template name="html-page">
        <html>
            <head>
                <link rel="stylesheet" type="text/css" href="../css/style.css"/>
            </head>
            <body>
                <div class="blockCenter">
                    <form action="/servlet">
                        <table>
                            <tr>
                                <td>
                                    Goods name:    
                                </td>
                                <td>
                                    <input type="text" name="name" value="{$name}" class="input"/>
                                </td>
                                <td>
                                    <div class="error">
                                        <xsl:value-of select="valid:getNameError($validator)"/>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Producer:    
                                </td>
                                <td>
                                    <input type="text" name="producer" value="{$producer}" class="input"/>
                                </td>
                                <td>
                                    <div class="error">
                                        <xsl:value-of select="valid:getProducerError($validator)"/>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Model(2 letters and 3 numbers):
                                </td>
                                <td>
                                    <input type="text" name="model" value="{$model}" class="input"/>
                                </td>
                                <td>
                                    <div class="error">
                                        <xsl:value-of select="valid:getModelError($validator)"/>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Date(dd-MM-yyyy):
                                </td>
                                <td>
                                    <input type="text" name="date" value="{$date}" class="input"/>
                                </td>
                                <td>
                                    <div class="error">
                                        <xsl:value-of select="valid:getDateError($validator)"/>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Color(in uppercase):
                                </td>
                                <td>
                                    <input type="text" name="color" value="{$color}" class="input"/>
                                </td>
                                <td>
                                    <div class="error">
                                        <xsl:value-of select="valid:getColorError($validator)"/>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Price:   
                                </td>
                                <td>
                                    <input type="text" name="price" value="{$price}" class="input"/>
                                </td>
                                <td>
                                    <div class="error">
                                        <xsl:value-of select="valid:getPriceError($validator)"/>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <div class="radio">
                                        <input type="radio" name="notInStock"/>Not in stock
                                    </div>
                                </td>
                            </tr>
                        </table>
                        <br/>
                        <input type="hidden" name="category" value="{$category}"/>
                        <input type="hidden" name="subcategory" value="{$subcategory}"/>
                        <input type="submit" name="command" value="SAVE" class="button"/>

                        <input type="submit" name="command" value="BACK" class="button"/>
                        <input type="hidden" name="page" value="goodsList"/>
                    </form>
                </div>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
